package org.lee.framework.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.solon.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.solon.plugins.inner.PaginationInnerInterceptor;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.solon.annotation.Db;
import org.lee.framework.config.extend.MyLogicSqlInjector;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;

import javax.sql.DataSource;

@Slf4j
@Configuration
public class DataSourceConfig {
    @Bean(value = "lee", typed = true)
    public DataSource lee(@Inject("${lee.master.db}") HikariDataSource ds) {
        log.info(ds.toString());
        return ds;
    }

    @Bean
    public void masterDb(@Db("lee") MybatisConfiguration cfg,
                         @Db("lee") GlobalConfig globalConfig) {
        MybatisPlusInterceptor plusInterceptor = new MybatisPlusInterceptor();
        plusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));

        cfg.setCacheEnabled(false);
        cfg.addInterceptor(plusInterceptor);

        globalConfig.setSqlInjector(new MyLogicSqlInjector());
    }

}
